<template>
  <div class="coupon_list-container">
    <breadcrumb style="position: absolute;width:100%;"></breadcrumb>
    <div class="content">
      <div class="query">
        <div class="margin_right_20">
          优惠券名称:&nbsp;
          <el-input placeholder="请输入优惠券名称" @keyup.enter.native="get_list(currentPage)" v-model="opt1" style="width:100px;height:30px;"></el-input>
        </div>
        <div class="margin_right_20">
          优惠券类型:&nbsp;
          <el-select v-model="opt2" @keyup.enter.native="get_list(currentPage)" style="width: 100px;">
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>

        <div class="margin_right_20">
          优惠券状态:&nbsp;
          <el-select v-model="opt3" @keyup.enter.native="get_list(currentPage)" style="width: 100px;">
            <el-option
              v-for="item in options3"
              :key="item.value"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>
        <div class="margin_right_20">
          有无时间限制:&nbsp;
          <el-select v-model="opt4" @keyup.enter.native="get_list(currentPage)" style="width: 100px;">
            <el-option
              v-for="item in options4"
              :key="item.value"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>

        <div class="btn">
          <el-button type="primary" icon="el-icon-search" @click="get_list(currentPage)">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="$router.push('addCoupon')">新增</el-button>
        </div>
      </div>
      <div class="table">
        <el-table :data="couponList" style="width: 100%" height="598">
          <el-table-column prop="name" label="优惠券名称"></el-table-column>
          <el-table-column prop="price" label="优惠券面额(元)"></el-table-column>
          <el-table-column prop="couponType" label="类型"></el-table-column>
          <el-table-column prop="take" label="已领取数量"></el-table-column>
          <el-table-column prop="used" label="已使用数量"></el-table-column>
          <el-table-column prop="startTime" label="开始时间"></el-table-column>
          <el-table-column prop="endTime" label="结束时间"></el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">
              <div v-if="scope.row.status == 0">{{'启用'}}</div>
              <div v-else>{{'禁用'}}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="small"
                @click="status_modify(scope.row.id,scope.row.status)"
              >{{scope.row.status=="0"?"禁用":"启用"}}</el-button>
              <!-- <el-button type="text" size="small" @click="jump_modify(scope.row.id)">修改</el-button> -->
              <!-- <el-button type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button> -->
              <el-button type="text" size="small" @click="jump_detail(scope.row.id)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :currentPage="currentPage"
          :page="page"
          :total="total"
          :pageSize="pageSize"
          @change-current="currentChange"
          @change-size="sizeChange"
        ></pagination>
      </div>
    </div>
  </div>
</template>

<script>
import breadcrumb from "../../components/breadcrumb";
import pagination from "../../components/page";

import { coupon_list, coupon_delete } from "../../api/getData.js";
export default {
  components: { breadcrumb, pagination },
  data() {
    return {
      form: {},
      //控制添加对话框的显示和隐藏
      dialogVisible: false,
      page: 1,
      currentPage: 1,
      total: 0,
      page: 1,
      pageSize: 10,
      opt1: "",
      opt2: "",
      opt3: "",
      opt4: "",
      // value4: "",
      // value5: "",
      options3: [
        { id: "", label: "全部" },
        { id: "1", label: "可使用" },
        // { id: "2", label: "禁用" },
        { id: "0", label: "已过期" },
        { id: "2", label: "未开始" }
      ],
      options2: [
        { id: "", label: "全部" },
        { id: "1", label: "全场通用" },
        { id: "2", label: "部分活动通用" },
        { id: "3", label: "部分商品类型通用" },
        { id: "4", label: "部分活动商品类型通用" },
        { id: "5", label: "部分商品可用" }
      ],
      options4: [
        { id: "", label: "全部" },
        { id: "0", label: "无时间限制" },
        { id: "1", label: "有时间限制" },
        { id: "2", label: "有效期" }
      ],

      couponList: [],
      couponMsg: { name: "", id: "" }
    };
  },
  created() {
    this.get_list();
  },
  methods: {
    sizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.get_list();
    },
    currentChange(val) {
      this.currentPage = val;
      this.get_list();
    },
    jump_detail(id) {
      this.$router.push("/coupon_detail?id=" + id);
    },
    jump_modify(id) {
      this.$router.push("/coupon_modify?id=" + id);
    },
    get_list(currentPage) {
      if(currentPage){
        this.currentPage = currentPage
      }
      let data="name=" +this.opt1+"&couponType=" +this.opt2+"&type="+this.opt3+"&current=" +this.currentPage+"&limitKind="+this.opt4+"&size="+this.pageSize
      // //console.log(data)
      coupon_list(data).then(result => {
        //console.log(result)
        this.total = result.data.data.total
        this.page = Math.ceil(this.total / this.pageSize)
        if (result.data.status == 0) {
          //console.log(result.data.data.records)
          this.couponList=result.data.data.records
          this.couponList.forEach(item=>{
            if(item.couponType=="1"){
              item.couponType="全场通用"
            }
            if(item.couponType=="2"){
              item.couponType="部分活动通用"
            }
            if(item.couponType=="3"){
              item.couponType="部分商品类型通用"
            }
            if(item.couponType=="4"){
              item.couponType="部分活动商品类型通用"
            }
            if(item.couponType=="5"){
              item.couponType="部分商品可用"
            }
          })
        }
      });
    },
    // addCoupon() {},
    handleDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let data = "id=" + id;
          coupon_delete(data).then(result => {
            if (this.total % this.pageSize == 1) {
              if (this.currentPage != 1) {
                this.currentPage--;
              }
            }
            this.get_list();
            return this.$message({
              type: "success",
              message: "删除成功"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    status_modify(id, status) {
      this.$confirm("此操作将修改状态, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios.post("/web_coupon/modify").then(result => {
            if (result.data.status == 0) {
              if (result.data.data == 0) {
                //console.log(status)
                if (status == "0") {
                  this.$message({
                    type: "success",
                    message: "禁用成功"
                  });
                } else {
                  this.$message({
                    type: "success",
                    message: "启用成功"
                  });
                }
                this.couponList.forEach(item => {
                  if (item.id == id) {
                    item.status = item.status == "1" ? "0" : "1";
                  }
                });
              } else {
                this.$message({
                  type: "warning",
                  message: "操作失败"
                });
              }
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消修改"
          });
        });
    },
    verfy_startTime() {
      if (this.value5) {
        this.timeCompare(1);
      }
      if (this.value4 == null) {
        this.value4 = "";
      }
    },
    //   校验结束时间
    verfy_endtime() {
      if (this.value4) {
        this.timeCompare(2);
      }
      if (this.value5 == null) {
        this.value5 = "";
      }
    },

    timeCompare(flag) {
      //  flag  用于标记是清楚开始时间，还是清楚结束时间
      if (new Date(this.value4).getTime() >= new Date(this.value5).getTime()) {
        if (flag == 1) {
          this.value4 = "";
        } else {
          this.value5 = "";
        }
        return this.$message({
          type: "warning",
          message: "'优惠券创建时间-开始'必须小于'优惠券创建时间-结束'"
        });
      }
    },
    select_() {
      this.$router.push("/selectCouponType");
    }
  }
};
</script>


<style lang="less" >
.coupon_list-container {
  @import "../../style/table.less";
  @import "../../style/content.less";
  display: flex;
  flex-flow: column;
  position: relative;
  height: 100%;
  .el-table .cell {
    white-space: pre-line !important;
  }
  .content {
    .query {
      .start_time {
        display: flex;
        align-items: center;
      }
      .end_time {
        display: flex;
        align-items: center;
      }
      .btn {
        .el-button {
          width: 70px;
        }
      }
    }
  }
}
</style>

